package com.woniu.controller;

import com.alibaba.excel.EasyExcel;
import com.woniu.entity.Constant;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Organization;
import com.woniu.pojo.Team;
import com.woniu.service.IOrganizationService;
import com.woniu.vo.OrganizationQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 * 机构表 前端控制器
 * </p>
 *
 * @author xiaotang
 * @since 2022-10-10
 */
@RestController
@RequestMapping("/organization")
@Api(tags = "机构管理")
@Slf4j
public class OrganizationController extends BaseController{

    @Autowired
    private IOrganizationService organizationService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("搜索+分页")
    @PostMapping("/search/{currentPage}/{pageSize}")
    public Result search(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody OrganizationQueryVo organizationQueryVo){
        PageResult pageResult = organizationService.search(currentPage, pageSize, organizationQueryVo);
        return toDataResult(pageResult);
    }

    @ApiOperation("增加")
    @PostMapping
    public Result add(@RequestBody Organization organization){
        boolean flag = organizationService.save(organization);
        if(flag){
            stringRedisTemplate.opsForSet().add(Constant.REDIS_DB_UPLOAD_IMAGE, organization.getImage());
        }
        return toResult(flag);
    }

    @ApiOperation("修改")
    @PutMapping
    public Result modify(@RequestBody Organization organization){
        boolean flag = organizationService.updateById(organization);
        if(flag){
            stringRedisTemplate.opsForSet().add(Constant.REDIS_DB_UPLOAD_IMAGE, organization.getImage());
        }
        return toResult(flag);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable Long id){
        boolean flag = organizationService.removeById(id);
        return toResult(flag);
    }

    @ApiOperation("根据机构id查询")
    @GetMapping("/{id}")
    public Result queryById(@PathVariable Long id){
        Organization organization = organizationService.getById(id);
        return toDataResult(organization);
    }

    @ApiOperation("根据机构id来修改机构状态")
    @PutMapping("/status/{organizationId}")
    public Result updateStatusById(@PathVariable Long organizationId){
        boolean flag = organizationService.updateStatusById(organizationId);
        return toDataResult(flag, "修改机构状态成功");
    }

    @ApiOperation("查询所有机构")
    @GetMapping
    public Result selectAll(){
        List<Organization> organizationList = organizationService.searchAll();
        return toDataResult(organizationList);
    }

    @ApiOperation("导出机构数据")
    @PostMapping("/export")
    public void download(HttpServletResponse response, @RequestBody OrganizationQueryVo organizationQueryVo) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        PageResult pageResult = organizationService.search(1, 100, organizationQueryVo);
        List<Organization> organizationList = (List<Organization>) pageResult.getData();


        EasyExcel.write(response.getOutputStream(), Organization.class).sheet("模板").doWrite(organizationList);
    }

    @ApiOperation("根据机构id查询所有团队")
    @GetMapping("/team/{organizationId}")
    public Result queryTeamByOrganizationId(@PathVariable Long organizationId){
        List<Team> teamList = organizationService.queryTeamByOrganizationId(organizationId);
        return toDataResult(teamList);
    }
}
